package cn.myfirefly.moming.integrate.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.myfirefly.moming.common.model.PositionDto;
import cn.myfirefly.moming.common.model.PositionFilterDto;
import cn.myfirefly.moming.common.service.PositionService;
import cn.myfirefly.core.BaseController;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;
import cn.myfirefly.utils.FilterUtil;

@Controller
@RequestMapping("position")
public class PositionController extends BaseController {

	@Autowired
	private PositionService positionService;
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		return "position/index";
	}
	
	@RequestMapping("edit.do")
	public String edit(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "position/edit";
	}
	
	@RequestMapping("add.do")
	public String add(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "position/edit";
	}
	
	@RequestMapping("save.do")
	@ResponseBody
	public ResultDto save(PositionDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		warpModifyInfo(dto, request);
		String msg = positionService.save(dto);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("保存成功");
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("list.do")
	@ResponseBody
	public PageResultDto list(PositionFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		FilterUtil.wrapFilterDto(dto, request);
		PageResultDto result = positionService.getList(dto);
		return result;
	}
	
	@RequestMapping("getPosition.do")
	@ResponseBody
	public ResultDto getPosition(PositionDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		PositionDto position = positionService.getPosition(dto.getPositionSn());
		return success(position);
	}
	
	@RequestMapping("del.do")
	@ResponseBody
	public ResultDto del(PositionDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String msg = positionService.del(dto.getPositionSn());
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("删除成功");
	}
	
	@RequestMapping("changeStatus.do")
	@ResponseBody
	public ResultDto changeStatus(PositionDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		warpModifyInfo(dto, request);
		String msg = positionService.changeStatus(dto);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success();
	}
	
}
